﻿using Haidou.Wpf.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Haidou.Wpf.UI.Util
{
	/// <summary>
	/// 垮线程UI
	/// </summary>
	public class Utils
	{


		public static void UITask<T>(Func<T> task, Action<T> uiAction)
		{
			System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((obj) =>
			{
				try
				{
					T? ret = task();
					Utils.UIBeginInvoke(uiAction, ret);
				}
				catch (Exception e)
				{
					LogWriter.Log("INI", "UITask", e.Message);

				}
			}), null);
		}
		public static System.Windows.Threading.DispatcherOperation UIBeginInvoke<T>(Action<T> action, T t)
		{
			return System.Windows.Application.Current.Dispatcher.BeginInvoke(action, t);
		}

		public static System.Windows.Threading.DispatcherOperation UIBeginInvoke(Action action)
		{
			return System.Windows.Application.Current.Dispatcher.BeginInvoke(action, null);
		}


		public static async Task UIBeginInvokeAsync(Action action)
		{
			// 使用Dispatcher.InvokeAsync或类似方法
			await System.Windows.Application.Current.Dispatcher.InvokeAsync(action);
		}


	}
}
